<template>
	<!-- 首页详情 -->
	<view class="detailsBox">
		<view class="detailsBox_top">
			<view class="item" v-for="(item,index) in showList" :key="index">
				<view class="itemName">{{item.name}}</view>
				<view><span class="itemNum">{{item.num}}</span><span class="itemUnit">{{item.unit}}</span> </view>
			</view>

		</view>
		<view class="detailsBox_center">
			<view class="detailsBox_center_name">曲线图</view>
			<view class="detailsBox_center_chartBox">
				<up-tabs :list="tabList" lineWidth="50" lineColor="#00AC79" :activeStyle="{
				            color: '#00AC79',
				            fontWeight: 'bold',
							fontSize:'18',
				            transform: 'scale(1.05)'
				        }" :inactiveStyle="{
				            color: '#606266',
				            transform: 'scale(1)',
							fontSize:'14'
				        }" itemStyle="padding-left: 15px; padding-right: 15px; height: 34px;" :current="tabCurrent"
					@click="tabClick">

				</up-tabs>


				<view class="neibox">
					<qiun-data-charts type="line" :opts="opts" :chartData="chartData" :canvas2d="true"
						canvasId="CanvasId" :ontouch="true" />
				</view>
			</view>
		</view>
		<view class="detailsBox_bottomBox">
			<!-- :style="[{height: equipmentList.value.length === 0 ? '400rpx' : ''}]" -->
			<view class="detailsBox_bottomBox_bottom" :style="[{height: isHeight}]">
				<view class="name">设备列表</view>
				<view class="itemBox">
					<view class="itemBox_item" v-for="(item, index) in equipmentList" :key="index">
						<view class="itemName">{{item.name}}</view>
						<view class="xq" @click="itemClick(item)">详情</view>
					</view>
				</view>

			</view>
		</view>
		<u-toast ref="uToast"></u-toast>
	</view>
</template>

<script setup>
	import {
		onMounted,
		ref,
		reactive,
		nextTick,
		getCurrentInstance
	} from 'vue'
	import {
		userInfoStore
	} from '/stores/index.js'
	const store = userInfoStore();
	let {
		proxy
	} = getCurrentInstance();
	import {
		onLoad,
		onReachBottom
	} from "@dcloudio/uni-app";
	let active = ref('0')
	// 顶部
	const getNum = (id) => {
		if (id === '0') {
			let opts = {
				url: proxy.$api.getRealTop,
				method: "POST",
			};
			proxy.$request.httpTokenRequest(opts).then((res) => {
				if (res.data.code === 200) {
					for (const key in res.data.data) {
						topList[0].list.map((item, index) => {
							if (key === item.field) {
								item.num = res.data.data[key]
							}
						})
					}


					showList.value = topList[0].list

				} else {
					proxy.$refs.uToast.show({
						type: 'error',
						message: res.data.msg
					})
				}
			}).catch((err) => {
				proxy.$refs.uToast.show({
					type: 'error',
					message: err
				})
			})
		} else {
			showList.value = topList[id].list
			console.log('showList.value',showList.value)
		}


	}
	let tabList = reactive([])
	// 图表
	const chartData = ref({
		categories: [],
		series: [{
			name: "",
			data: []
		}]
	})
	const opts = ref({
		color: ["#00AC79"],
		padding: [20, 10, 0, 10],
		enableScroll: true,
		legend: {},
		xAxis: {
			disableGrid: true,
			scrollShow: true,
			itemCount: 6
		},
		yAxis: {
			gridType: "dash",
			dashLength: 2
		},
		extra: {
			line: {
				type: "curve",
				width: 2,
				activeType: "hollow"
			}
		}
	})
	const tabCurrent = ref(0)
	const tabClick = (item) => {
		tabCurrent.value = item.index
		getServerData(active.value, getCurrentDate())


	}
	const getServerData = (id, date) => {
		if (id === '0') {
			let opts = {
				url: `${proxy.$api.getRealCharts}?date=${date}`,
				method: "POST",
			};
			proxy.$request.httpTokenRequest(opts).then((res) => {
				if (res.data.code === 200) {
					res.data.data.time=res.data.data.time.map(datetime => datetime.slice(11, 16));
					tabList.map((item, index) => {
						chartData.value.categories = res.data.data.time
						
						chartData.value.series = [{
							name: tabList[tabCurrent.value].name,
							data: res.data.data[tabList[tabCurrent.value].fild].map(Number)
						}]
					})



				} else {
					proxy.$refs.uToast.show({
						type: 'error',
						message: res.data.msg
					})
				}
			}).catch((err) => {
				proxy.$refs.uToast.show({
					type: 'error',
					message: err
				})
			})
		} else {
			setTimeout(() => {
				chartData.value.categories = ["00:00", "00:05", "00:10", "00:15", "00:20", "00:25", "00:30",
					"00:35", "00:40"
				]
				chartData.value.series = [{
					name: tabList[tabCurrent.value].name,
					data: [0, 0, 0, 0, 0, 0, 0, 0, 0]
				}]


			}, 500);
		}








	}
	// 底部
	let isHeight = ref('')
	let equipmentList = ref([])
	let fackData = reactive({
		'0': [],
		// '1': [
		// {
		// 	transformerSn: '3411134473',
		// 	name: '逆变器1',
		// 	power: '7500',
		// 	company: '邯郸市永年区虎滕紧固件制造有限公司',
		// 	address: '小马龙乡到固村北'
		// }, {
		// 	transformerSn: '3411134478',
		// 	name: '逆变器2',
		// 	power: '7500',
		// 	company: '邯郸市永年区虎滕紧固件制造有限公司',
		// 	address: '小马龙乡到固村北'
		// }, {
		// 	transformerSn: '3411134479',
		// 	name: '逆变器3',
		// 	power: '7500',
		// 	company: '邯郸市永年区虎滕紧固件制造有限公司',
		// 	address: '小马龙乡到固村北'
		// }, {
		// 	transformerSn: '3411134410',
		// 	name: '逆变器4',
		// 	power: '7500',
		// 	company: '邯郸市永年区虎滕紧固件制造有限公司',
		// 	address: '小马龙乡到固村北'
		// }, {
		// 	transformerSn: '3411134423',
		// 	name: '逆变器5',
		// 	power: '7500',
		// 	company: '邯郸市永年区虎滕紧固件制造有限公司',
		// 	address: '小马龙乡到固村北'
		// }],
		// '2': [{
		// 	transformerSn: '2411134473',
		// 	name: 'PCS1',
		// 	power: '7800',
		// 	company: '邯郸市永年区虎滕紧固件制造有限公司',
		// 	address: '小马龙乡到固村北'
		// }, {
		// 	transformerSn: '2411134475',
		// 	name: 'PCS2',
		// 	power: '7800',
		// 	company: '邯郸市永年区虎滕紧固件制造有限公司',
		// 	address: '小马龙乡到固村北'
		// }, {
		// 	transformerSn: '2411134425',
		// 	name: 'PCS3',
		// 	power: '7800',
		// 	company: '邯郸市永年区虎滕紧固件制造有限公司',
		// 	address: '小马龙乡到固村北'
		// }, {
		// 	transformerSn: '2411134435',
		// 	name: 'PCS4',
		// 	power: '7800',
		// 	company: '邯郸市永年区虎滕紧固件制造有限公司',
		// 	address: '小马龙乡到固村北'
		// }, {
		// 	transformerSn: '2411134423',
		// 	name: 'PCS5',
		// 	power: '7800',
		// 	company: '邯郸市永年区虎滕紧固件制造有限公司',
		// 	address: '小马龙乡到固村北'
		// }],
		// '3': [{
		// 	transformerSn: '1411134425',
		// 	name: '充电桩1',
		// 	power: '8000',
		// 	company: '邯郸市永年区虎滕紧固件制造有限公司',
		// 	address: '小马龙乡到固村北'

		// }, {
		// 	transformerSn: '1411134423',
		// 	name: '充电桩2',
		// 	power: '8000',
		// 	company: '邯郸市永年区虎滕紧固件制造有限公司',
		// 	address: '小马龙乡到固村北'
		// }, {
		// 	transformerSn: '1411134425',
		// 	name: '充电桩3',
		// 	power: '8000',
		// 	company: '邯郸市永年区虎滕紧固件制造有限公司',
		// 	address: '小马龙乡到固村北'
		// }, {
		// 	transformerSn: '1411134421',
		// 	name: '充电桩4',
		// 	power: '8000',
		// 	company: '邯郸市永年区虎滕紧固件制造有限公司',
		// 	address: '小马龙乡到固村北'
		// }, {
		// 	transformerSn: '1411134422',
		// 	name: '充电桩5',
		// 	power: '8000',
		// 	company: '邯郸市永年区虎滕紧固件制造有限公司',
		// 	address: '小马龙乡到固村北'
		// }],

	})
	// 设备列表
	const getEquipmentList = (id) => {
	
		if (id === '0') {
			isHeight.value=''
			// 用电
			let opts = {
				url: proxy.$api.getDeviceList,
				method: "POST",
			};
			proxy.$request.httpTokenRequest(opts).then((res) => {
				if (res.data.code === 200) {
					fackData['1'] = res.data.data

					fackData['1'].map((item, index) => {
						item.name = item.transformerSn
					})



					equipmentList.value = fackData['1']
					
				} else {
					proxy.$refs.uToast.show({
						type: 'error',
						message: res.data.msg
					})
				}
			}).catch((err) => {
				proxy.$refs.uToast.show({
					type: 'error',
					message: err
				})
			})
		} else {
			equipmentList.value = fackData[id]
			isHeight.value='400rpx'
			
		}





	}
	// 详情
	const itemClick = (item) => {

		if (active.value === '0') {
			uni.navigateTo({
				url: `/pagesA/indexDetails/indexDetails?item=${encodeURIComponent(JSON.stringify(item))}`
			})
		} else {
			uni.navigateTo({
				url: `/pagesA/otherDeails/otherDeails?item=${encodeURIComponent(JSON.stringify(item))}`
			})
		}


	}
	let showList = ref([])
	const topList = reactive([{
			topId: '0',
			list: [{
				name: '变压器总功率',
				field: 'capacity',
				num: '',
				unit: 'KVA'
			}, {
				name: '总有功P',
				field: 'P',
				num: '',
				unit: 'Kw'
			}, {
				name: '总无功Q',
				field: 'Q',
				num: '',
				unit: 'Kvar'
			}, {
				name: '总视在S',
				field: 'Stot',
				num: '',
				unit: 'Kva'
			}]
		},
		{
			topId: '1',
			list: [{
					name: '总装机容量',
					field: '',
					num: '0',
					unit: 'Kw'
				},
				{
					name: '总发电功率P',
					field: '',
					num: '0',
					unit: 'Kw'
				},

			]
		},
		{
			topId: '2',
			list: [{
					name: '总装机容量',
					field: '',
					num: '0',
					unit: 'Kw'
				},
				{
					name: '充电功率P',
					field: '',
					num: '0',
					unit: 'Kw'
				},
				{
					name: '放电功率P',
					field: '',
					num: '0',
					unit: 'Kw'
				},
				{
					name: '电池电量',
					field: '',
					num: '0',
					unit: 'Kwh'
				}
			]
		},
		{
			topId: '3',
			list: [{
					name: '总装机容量',
					field: '',
					num: '0',
					unit: 'Kw'
				},
				{
					name: '充电功率P',
					field: '',
					num: '0',
					unit: 'Kw'
				},
			]
		}
	])
	const getCurrentDate = () => {
		const now = new Date();
		const year = now.getFullYear();
		const month = now.getMonth() + 1; // 月份是从0开始的，所以需要加1
		const day = now.getDate();
		return `${year}-${month.toString().padStart(2, '0')}-${day.toString().padStart(2, '0')}`;
	}
	onLoad((option) => {
		nextTick(() => {
			tabList = []
			// JSON.parse(decodeURIComponent(option.item)).name
			uni.setNavigationBarTitle({
				title: store.userInfo.remark
			});
			active.value = JSON.parse(decodeURIComponent(option.item)).id
			if (active.value === '0') {
				tabList.push({
						name: '总有功功率',
						fild: 'p'
					}, {
						name: '总无功功率',
						fild: 'q'
					}, {
						name: '总视在功率',
						fild: 'stot'
					}, {
						name: '负荷率',
						fild: 'stot-capacity'
					},


				)
			} else {
				topList[active.value].list.map((item, index) => {
					if (item.name != '总装机容量') {
						tabList.push({
							name: item.name
						})
					}
				})
				tabCurrent.value = 0
				getServerData(active.value, getCurrentDate())
			}

			getNum(active.value)
			getEquipmentList(active.value)
			getServerData(active.value, getCurrentDate())


		})

	})
</script>
<style>
	page {
		background-color: #F6F6F6;
	}
</style>
<style lang="scss">
	.detailsBox {
		padding-bottom: 30rpx;
		box-sizing: border-box;

		&_top {
			width: 100%;
			height: 360rpx;
			background: linear-gradient(to right, #00AC79, #066B4E);
			padding: 32rpx 82rpx;
			box-sizing: border-box;
			display: flex;
			align-items: center;
			justify-content: space-between;
			flex-wrap: wrap;
			text-align: center;

			.item {
				flex: 50%;

				.itemName {
					font-size: 28rpx;
					color: rgba(255, 255, 255, 0.5);
					margin-bottom: 8rpx;
				}

				.itemNum {
					font-size: 36rpx;
					color: #FFFFFF;
				}

				.itemUnit {
					margin-left: 18rpx;
					font-size: 24rpx;
					color: #FFFFFF;
				}
			}

		}

		&_top:after {
			content: '';
			flex: 50%;
		}

		&_center {
			padding: 32rpx 16rpx;
			box-sizing: border-box;

			&_name {
				font-weight: 550;
				font-size: 32rpx;
				color: #000000;
				margin-bottom: 24rpx;
			}

			&_chartBox {


				width: 100%;
				height: 466rpx;
				background-color: #FFFFFF;
				box-shadow: 0rpx 2rpx 4rpx 2rpx rgba(0, 0, 0, 0.16);
				border-radius: 8rpx 8rpx 8rpx 8rpx;

				.xuanz {
					padding: 0 20rpx;
					box-sizing: border-box;
					margin-bottom: 30rpx;
					display: flex;

				}

				.neibox {
					width: 100%;
					height: 366rpx;
				}
			}
		}

		&_bottomBox {
	
			padding: 0 16rpx;
			box-sizing: border-box;

			&_bottom {
				
				background-color: #FFF;
				text-align: center;
				box-shadow: 0rpx 2rpx 6rpx 2rpx rgba(0, 0, 0, 0.16);
				border-radius: 0rpx 0rpx 0rpx 0rpx;
				padding: 20rpx 16rpx 48rpx 16rpx;
				box-sizing: border-box;

				.name {
					font-size: 32rpx;
					color: #000000;
					font-weight: 550;
					margin-bottom: 34rpx;
				}

				.itemBox {
					padding: 0 16rpx;
					box-sizing: border-box;

					.itemBox_item {
						height: 98rpx;
						line-height: 98rpx;
						// padding-bottom: 28rpx;
						border-bottom: 2rpx dashed #E9E9E9;
						box-sizing: border-box;
						display: flex;
						justify-content: space-between;
						align-items: center;

						.itemName {
							font-size: 24rpx;
							color: #333333;
						}

						.xq {
							font-size: 28rpx;
							color: #00AC79;
						}


					}

					.itemBox_item:last-child {
						height: 98rpx;
						line-height: 98rpx;
						// padding-bottom: 28rpx;
						border-bottom: 0rpx dashed red;
						box-sizing: border-box;
						display: flex;
						justify-content: space-between;
						align-items: center;

						.itemName {
							font-size: 24rpx;
							color: #333333;
						}

						.xq {
							font-size: 28rpx;
							color: #00AC79;
						}


					}




				}


			}

		}
	}
</style>